package com.servlet.music;

import com.dao.EchartsService;
import com.dao.ManagerService;
import com.dao.impl.EchartsServiceImpl;
import com.dao.impl.ManagerServiceImpl;
import com.entity.Echarts;
import com.entity.vo.CircleEchartsVO;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.servlet.teacher.BaseServlet;
import com.util.JWTTokenUtil;
import com.util.JsonUtil;
import com.util.ProxyUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页信息展示
 */
@WebServlet("/EchartsServlet")
public class EchartsServlet extends BaseServlet {

	/**
	 * 管理员首页的顶部数据
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getTop(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		EchartsService echartsService = (EchartsService) ProxyUtil.getDaoProxy(new EchartsServiceImpl());


		List<Map<String, String>> ans = null;
		try {
			Echarts result= echartsService.getTop();

			ans = transToResult(result);

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(ans);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 获取圆的图表的数据
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getEchartsCirle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		EchartsService echartsService = (EchartsService) ProxyUtil.getDaoProxy(new EchartsServiceImpl());


		List<CircleEchartsVO> ans = null;
		try {

			 ans =  echartsService.getEchartsCirle();

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(ans);
		out.print(gson.toJson(jsonUtil));
	}



	/**
	 * 将对象返回成数组
	 * @param ans
	 */
	private List<Map<String, String>> transToResult(Echarts ans) {
		// 处理结果封装为数组，五个
		List<Map<String, String>> result = new ArrayList<>();
		HashMap<String, String> item1 = new HashMap<>();
		item1.put("label", ans.getOnLineUser());
		item1.put("icon", ans.getOnLineUserIcon());
		item1.put("num", ans.getUserNum()+"");

		HashMap<String, String> item2 = new HashMap<>();
		item2.put("label", ans.getExitsSonger());
		item2.put("icon", ans.getExitsSongerIcon());
		item2.put("num", ans.getSongerNum()+"");

		HashMap<String, String> item3 = new HashMap<>();
		item3.put("label", ans.getExitsSong());
		item3.put("icon", ans.getExitsSongIcon());
		item3.put("num", ans.getSongNum()+"");

		HashMap<String, String> item4 = new HashMap<>();
		item4.put("label", ans.getExistSongList());
		item4.put("icon", ans.getExistSongListIcon());
		item4.put("num", ans.getSongListNum()+"");

		HashMap<String, String> item5 = new HashMap<>();
		item5.put("label", ans.getExitsComment());
		item5.put("icon", ans.getExitsCommentIcon());
		item5.put("num", ans.getCommentNum()+"");

		result.add(item1);
		result.add(item2);
		result.add(item3);
		result.add(item4);
		result.add(item5);

		return result;
	}
}
